#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using Thn.Data.Database.Sql.Dialect;
using System.Data;
using MySql.Data.MySqlClient;
using Thn.Data.Exceptions;
using System.Collections;
#endregion

namespace Thn.Data.Database.Sql.MySql
{
    /// <summary>
    /// Dialect specifications for MySql database
    /// </summary>
    public class MySqlDialect : AbstractDialect
    {
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public MySqlDialect()
        {
            this.mSqlStrings = new MySqlDialectStrings();
        }
        #endregion

        #region Create Connection
        /// <summary>
        /// Construct a database specific connection instance
        /// </summary>
        public override IDbConnection CreateConnection()
        {
            return new MySqlConnection();
        }
        #endregion

        #region Create command
        /// <summary>
        /// Construct a database specific command instance
        /// </summary>
        public override IDbCommand CreateCommand()
        { return new MySqlCommand(); }
        #endregion

        #region Create Parameter
        /// <summary>
        /// Construct a database specific parameter instance
        /// </summary>
        /// <param name="paramName">Name of parameter</param>
        /// <param name="valueType">.NET type of parameter's value</param>
        public override IDataParameter CreateParameter(string paramName, Type valueType)
        {
            MySqlParameter result = null;
            Hashtable typeMap = this.TypeMap;

            if (valueType.IsEnum) result = new MySqlParameter(paramName, MySqlDbType.Int32);
            else if (typeMap.Contains(valueType)) result = new MySqlParameter(paramName, (MySqlDbType)typeMap[valueType]);
            else if (!valueType.IsPrimitive && valueType.IsClass)
            {
                result = new MySqlParameter(paramName, MySqlDbType.LongText);
            }
            else
            {
                //throw exception when this dialect doesn't support the provided value type
                TypeNotSupportedException.Publish(valueType);
            }

            return result;
        }
        #endregion


        #region Fixed Length String
        /// <summary>
        /// Returns the equivalent data type when a string attribute has specified size
        /// </summary>
        protected override object GetFixedLengthStringType()
        {
            return MySqlDbType.VarChar;
        }

        /// <summary>
        /// Returns the string version of equivalent data typep when a string attribute has specified size
        /// </summary>
        protected override string GetFixedLengthStringTypeAsString()
        {
            return "Text";
        }
        #endregion

        #region Map Types
        /// <summary>
        /// Maps .NET types to data types.
        /// </summary>
        /// <param name="typeMap">key: .NET type, value: data type</param>
        /// <param name="stringMap">key: .NET type, value: data type as string</param>
        protected override void MapTypes(Hashtable typeMap, Hashtable stringMap)
        {
            typeMap.Add(typeof(DateTime), MySqlDbType.Datetime);
            typeMap.Add(typeof(Guid), MySqlDbType.Text);
            typeMap.Add(typeof(int), MySqlDbType.Int32);
            typeMap.Add(typeof(long), MySqlDbType.Int64);
            typeMap.Add(typeof(short), MySqlDbType.Int16);
            typeMap.Add(typeof(decimal), MySqlDbType.Decimal);
            typeMap.Add(typeof(double), MySqlDbType.Double);
            typeMap.Add(typeof(string), MySqlDbType.Text);
            typeMap.Add(typeof(float), MySqlDbType.Float);
            typeMap.Add(typeof(Enum), MySqlDbType.Enum);
            typeMap.Add(typeof(byte), MySqlDbType.Byte);
            typeMap.Add(typeof(bool), MySqlDbType.Bit);

            stringMap.Add(typeof(DateTime), "datetime");
            stringMap.Add(typeof(Guid), "text");
            stringMap.Add(typeof(int), "int");
            stringMap.Add(typeof(long), "bigint");
            stringMap.Add(typeof(short), "smallint"); 
            stringMap.Add(typeof(decimal), "decimal");
            stringMap.Add(typeof(double), "double");
            stringMap.Add(typeof(string), "text");
            stringMap.Add(typeof(float), "float");
            stringMap.Add(typeof(Enum), "enum");
            stringMap.Add(typeof(byte), "byte");
            stringMap.Add(typeof(bool), "bit");
            
        }
        #endregion

        #region Parse Exception
        /// <summary>
        /// Parse a database specific exception to standard one (known to this data layer)
        /// </summary>
        /// <param name="error">original exception</param>
        /// <returns>standardized exception</returns>
        protected override DataExceptionBase OnParseException(Exception error)
        {
            DataExceptionBase result = new DataExceptionBase(error);

            return result;
        }
        #endregion
        
    }
}
